package com.aierkeji.call.b2c.schedule;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.aierkeji.call.b2c.queue.StatisticsQueue;
import com.aierkeji.call.b2c.service.AkjCallClueStatisticsService;
import com.aierkeji.call.b2c.util.AppConfig;
import com.aierkeji.call.b2c.util.DateTimeUtils;

@Component
public class Schedule {

	private final Logger log = LogManager.getLogger(getClass());

	
	@Autowired
	AkjCallClueStatisticsService akjCallClueStatisticsService;

	boolean updateStatisticsFlag = false;
	boolean queueFlag = false;

	@PostConstruct
	public void init() {
		StatisticsQueue.build(Integer.parseInt(AppConfig.getProperty("updateStatisticsMaxCount", "30")));
	}
	
	@Scheduled(cron = "0 0 0,12,17 * * ?")
	private void updateStatistics() {

		log.info("---------------------------开始启动存储过程!------------------------" + DateTimeUtils.getCurrentTime()
				+ "------,updateStatisticsFlag:{},queueFlag:{}", updateStatisticsFlag, queueFlag);
		if (updateStatisticsFlag || queueFlag) {
			log.info("last Scheduled is running ,updateStatisticsFlag:{},queueFlag:{}", updateStatisticsFlag,
					queueFlag);
			return;
		}
		updateStatisticsFlag = true;

		Map<String, Object> map = new HashMap<String, Object>();
		String speciallyTime = "";
		Date nowDate = DateTimeUtils.getCurrentTime();

		speciallyTime = DateTimeUtils.converDateToString(DateTimeUtils.getDayBefore(nowDate),
				DateTimeUtils.DATE_PATTERN_TIMESTAMP);

		String hourString = speciallyTime.substring(11, 13);
		if (!hourString.equals("00")) {
			speciallyTime = "";
		}

		map.put("speciallyTime", speciallyTime);
		try {
			akjCallClueStatisticsService.updateStatistics(map);
		}catch(Exception e) {
			log.error("Schedule updateStatistics errror --- map:{}", map, e);
		}
		

		updateStatisticsFlag = false;
		log.info("---------------------------结束存储过程!------------------------" + DateTimeUtils.getCurrentTime());
	}

	/**
	 * 处理页面发起更新统计数据请求
	 */
	@Scheduled(cron = "*/5 * * * * ?")
	private void consumeStatisticsQueue() {
		if(queueFlag) {
			
			log.info("consumeStatisticsQueue flag is true:{}",DateTimeUtils.getCurrentTime());
			return;
		}
		int size = StatisticsQueue.linkedBlockingQueue.size();
		if (size > 0) {
			log.info("StatisticsQueue.linkedBlockingQueue.size:{},start consumeStatisticsQueue----{}", size,DateTimeUtils.getCurrentTime());
			queueFlag = true;
			Map<String,Object> map = StatisticsQueue.linkedBlockingQueue.poll();
			try {
				akjCallClueStatisticsService.updateStatistics(map);
			}catch(Exception e) {
				log.error("Schedule updateStatistics errror --- map:{}", map, e);
			}
			log.info("queue map---:{}",map);
			queueFlag = false;
			log.info("end consumeStatisticsQueue Time----{}",DateTimeUtils.getCurrentTime());

		} else {
//			log.info("StatisticsQueue.linkedBlockingQueue.isEmpty");

		}

	}

	/*
	 * public static void main(String[] args) {
	 * 
	 * String speciallyTime = ""; Date nowDate = DateTimeUtils.getCurrentTime();
	 * 
	 * speciallyTime =
	 * DateTimeUtils.converDateToString(DateTimeUtils.getDayBefore(nowDate),
	 * DateTimeUtils.DATE_PATTERN_TIMESTAMP);
	 * 
	 * String hourString = speciallyTime.substring(11, 13);
	 * 
	 * System.out.println(hourString); }
	 */
}
